package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.dao.RoleMenuDao;
import com.example.domain.RoleMenu;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.RoleMenuService;
import com.example.tool.DateTool;

@Service
public class RoleMenuServiceImpl implements RoleMenuService {

    @Autowired
    RoleMenuDao roleMenuDao;

    /**
     * @describe 添加角色菜单信息
     */
    @Override
    public Result addRoleMenu(String json) {
        JSONObject parseObject = JSON.parseObject(json);
        String[] menuId = parseObject.getString("menuId").split(",");
        String[] roleId = parseObject.getString("roleId").split(",");
        List<RoleMenu> list = new ArrayList<RoleMenu>();
        if (menuId != null && roleId != null) {
            for (int i = 0; i < menuId.length; i++) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(Integer.parseInt(roleId[0]));
                roleMenu.setMenuId(Integer.parseInt(menuId[i]));

                // 获取系统当前时间作为记录创建时间
                roleMenu.setRecordTime(DateTool.getTimestamp());
                list.add(roleMenu);
            }

        }
        // 调用JPA保存实体方法
        List<RoleMenu> roleMenu1 = roleMenuDao.saveAll(list);

        if (roleMenu1 != null) {
            return ResultFactory.buildSuccessResult(roleMenu1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新角色菜单信息
     */
    @Override
    public Result updateRoleMenu(RoleMenu roleMenu) {
        if (roleMenu != null) {
            // 获取系统当前时间作为记录更新时间
            roleMenu.setUpdateTime(DateTool.getTimestamp());
        }
        // 调用JPA保存并刷新实体方法
        RoleMenu roleMenu1 = roleMenuDao.saveAndFlush(roleMenu);

        if (roleMenu1 != null) {
            return ResultFactory.buildSuccessResult(roleMenu1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据角色菜单编号删除角色菜单信息
     */
    @Override
    public Result deleteRoleMenuById(String roleMenuId) {
        int row = roleMenuDao.deleteByRoleMenuId(Integer.parseInt(roleMenuId));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有角色菜单信息
     */
    @Override
    public Result queryAllRoleMenu() {
        List<RoleMenu> list = roleMenuDao.findAll();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号查询角色菜单信息
     */
    @Override
    public Result queryRoleMenuById(String roleMenuId) {
        List<RoleMenu> list = roleMenuDao.findByRoleMenuId(Integer.parseInt(roleMenuId));
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加角色菜单信息
     */
    @Override
    public Result addRoleMenuByExcel(List<RoleMenu> list) {
        List<RoleMenu> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = roleMenuDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据角色菜单编号批量删除角色菜单信息
     */
    @Override
    public Result deleteRoleMenuByIds(Map<String, String> ids) {
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (ids != null) {
            String idstr = ids.get("Ids");
            String[] id = idstr.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = roleMenuDao.deleteByRoleMenuIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 角色菜单信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        List<Map<String, Object>> list = null;

        if (info[0].equals("roleName")) {

            list = roleMenuDao.findByRoleNameLike(info[1]);

        } else if (info[0].equals("menuName")) {

            list = roleMenuDao.findByMenuNameLike(info[1]);

        }

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有未分配的角色名称
     */
    @Override
    public Result queryRoleNameNotAllocation() {
        List<Map<String, Object>> list = roleMenuDao.queryRoleNameNotAllocation();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有角色权限已分配信息
     */
    @Override
    public Result queryAllRoleAndMenuInfo() {
        List<Map<String, Object>> list = roleMenuDao.queryAllRoleAndMenuInfo();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

}
